* Tablesb-DiD.do
*

* Global options.
global OUTREG2OPTIONS tex(fragment) nocons 2aster auto(2) label alpha(.05, .1) //symbol(**,*)
global TABLELOC "../tables"
global FECHECK "\checkmark"
global STANDARDIZEDV 1 // standard normal continuous DVs?
global PRES_SHARE_CONTROL "pooled_dem_2p_pres" // Include control for presidential share in district
global ROBUST_SE "vce(robust)"

*********************
* Function to standardize.
*********************
capture program drop standardizeVar2
program define standardizeVar2
  * Standardize a variable into SD units.
  * Arguments.
  *  varname -- variable to standardize
  *  name -- new name for variable
  *  num_sds -- number of SDs to use (default 2)
  syntax varname [if], Generate(name) [ num_sds(real 2) ]
  * Drop new_varname
  qui capture drop `generate'
  qui summ `varlist' `if'
  qui gen `generate' = `varlist'/(`num_sds'*r(sd)) `if'
  * Label new variable as standardized.
  local templabel : var label `varlist'
  qui label var `generate' "`templabel' (standardized)"   
end

****************
* Data from Merge_Nominees_To_Incums.R.
****************
local dset "Primaries_Nom"
di _n _n "WORKING WITH DATA SET `dset'" _n _n
* Data created in Create*.R script.
import delimited using "../processedData/`dset'.csv", delim(",") clear
  
**************
* Recodes.
**************
* Rescale primary votes.
capture qui replace tot_prim_votes = tot_prim_votes/1000
if (inlist("`dset'","Primaries_Nom","Primaries_Contribs_Nom")) {
  * Log counts.
  foreach var of varlist party_donors cong_donors {
    capture qui replace `var' = log(`var' + 1)
  }
}

* Presidential vote share.
* Recode share from percent to proportion to be closer in units to NOMINATE/CF.
qui replace dem_2p_pres = dem_2p_pres/100
* Create a pooled dem share so that larger values indicates more friendly
* to nominee party higher.
qui g pooled_dem_2p_pres = dem_2p_pres
qui replace pooled_dem_2p_pres = 1-dem_2p_pres if nominee_party == "R"
* Era variable.
qui gen later_era = year >= 1994

* ``Competitive primary'' := more than one cand receives votes.
qui gen multiple_cands = n_w_prim_votes > 1 & n_w_prim_votes != .

* Set nominee ideology variables to missing for non-competitive primaries.
di _n "Setting nominee ideology variables to missing for non-competitive primaries." _n
foreach var of varlist prehsscore hsscore recipientcfscore {
  qui replace `var' = . if inlist(multiple_cands, 0, .)
}
di _n "Setting CF Score outliers to missing." _n
qui replace recipientcfscore = . if !inrange(recipientcfscore , -2.5,2)

*******************
* Panel variables.
*******************
* Grouping variable: Party district cycle FEs.
qui egen grouping_var = group(nominee_party district_dist_cycle)

* Time variable: Party year FEs.
qui egen time_var = group(nominee_party year)


**************
* Label variables.
**************
qui do _LabelVariables.do


* Set polarity of scores.
if (1) {
  * Change direction of NOMINATE/HS/CF so always coded in centrist direction.
  * Republican challengers original coding HS/CF is positive means more extreme.
  * Mirror so that positive means more centrist.
  capture qui g orig_recipientcfscore = recipientcfscore
  capture qui replace recipientcfscore = -1*recipientcfscore if nominee_party == "R"
  capture label var recipientcfscore "Nominee, centrism, (CF Score)" 
  
  capture qui g orig_hsscore = hsscore
  capture qui replace hsscore = -1*hsscore if nominee_party == "R"
  capture label var hsscore "Nominee, centrism" 
  capture qui g orig_prehsscore = prehsscore
  capture qui replace prehsscore = -1*prehsscore if nominee_party == "R"
  capture label var prehsscore "Nominee, centrism, (pre-primary, ideology)" 
  
  capture qui g orig_mean_party_hsscore = mean_party_hsscore
  capture qui replace mean_party_hsscore = -1*mean_party_hsscore if nominee_party == "R"
  capture label var mean_party_hsscore "Mean party, support, centrism" 

  capture label var n_centrist_cand "Number, of centrist, candidates" 
  capture label var n_extreme_cand "Number, of extreme, candidates" 
  capture label var en_cands "Effective, number of, candidates" 

  * Republican incumbents original coding NOMINATE is positive more extreme.
  * Mirror so that positive means more centrist.
  qui g orig_incum_nom_d1 = incum_nom_d1
  qui replace incum_nom_d1 = -1*incum_nom_d1 if nominee_party == "D"
  label var incum_nom_d1 "Incumbent centrism" 

}

* Interaction with competitive after setting polarity.
qui gen incum_nom_d1Xcomp = incum_nom_d1*lag_general_compet
label var incum_nom_d1Xcomp "Incumbent*Competitive"
qui gen extreme_incumXcomp = extreme_incum*lag_general_compet
qui gen centrist_incumXcomp = centrist_incum*lag_general_compet
label var extreme_incumXcomp "Incumbent extreme*Competitive" 
label var centrist_incumXcomp "Incumbent centrist*Competitive" 
quie gen pooled_presXcomp = pooled_dem_2p_pres*lag_general_compet
label var pooled_presXcomp "Out-party presidential share*Competitive" 

* Interaction with Dem nominee after setting polarity.
qui gen incum_nom_d1Xdem = incum_nom_d1*(nominee_party == "D")
label var incum_nom_d1Xdem "Incumbent*Democratic primary"
quie gen pooled_presXdem = pooled_dem_2p_pres*(nominee_party == "D")
label var pooled_presXdem "Out-party presidential share*Democratic primary" 

* Interaction with post-1994 after setting polarity.
qui gen incum_nom_d1Xafter94 = incum_nom_d1*(year > 1994)
label var incum_nom_d1Xafter94 "Incumbent*After 1994"
quie gen pooled_presXafter94 = pooled_dem_2p_pres*(year > 1994)
label var pooled_presXafter94 "Out-party presidential share*After 1994" 

* Save out file with recoded variables for use in R.
export delimited using "../processedData/StataProcessed-`dset'.csv", delim(",") replace

*
* %%%%%%%%%%%%%%%%%%%
* Diff in diffs.
* %%%%%%%%%%%%%%%%%%%
*

******************
* Main models.
******************

* Dependent variables DVs.
local DVS "n_fec_cands n_centrist_cand n_extreme_cand cong_donors mean_party_hsscore multiple_cands en_cands hsscore"

* Set up local variables.
local outregfile "`dset'-DiD"
local outregfileByParty "`dset'-DiD-By-Party"
local outregfileByEra "`dset'-DiD-By-Era"
local outregfileByComp "`dset'-DiD-By-Competitive"
local notes ""
local repl "replace"
local replByParty "replace"
local replByComp "replace"
local replByEra "replace"
di _n "`outregfile'.tex"
* Order of coefficients.
local sortvar = "incum_nom_d1 ev2 n_fec_cands"
local sortvarcomp = "incum_nom_d1 ev2 incum_nom_d1Xcomp ev2Xcomp lag_general_compet"

* Loop over DVS.
foreach dv of varlist `DVS' {
  
  * Number of decimals.
  if (regexm("`dv'","contribs|support")) {
    local decimals = 1
  }
  else {
    local decimals = 3
  }

  * Column title.
  local ctitle: var label `dv'
    
    ********************
    * Models pooled across party.
    ********************
    di "Pooled: `dv' `repl'"
    * Don't execute if no data.
    qui summ `dv'
    if (r(N) > 0) {
      
      ****************
      * Time and group var FEs.
      ****************
      qui reghdfe `dv' incum_nom_d1 $PRES_SHARE_CONTROL, a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE

			* Recode continuous NOMINATE explanatory variable into Gelman 2SD units
			* for easier interpretation.
			standardizeVar2 incum_nom_d1, generate(ev2)

			* Re-run regression on standardized variable.
			qui reghdfe `dv' ev2 $PRES_SHARE_CONTROL, a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE
			
       * Outreg.
       if ($STANDARDIZEDV & inlist("`dv'", "hsscore", "prehsscore" ,"nominee_nominate" ,"recipientcfscore", "mean_party_hsscore")) {
        * Standardize coefficients.
        qui summ `dv' if e(sample)
        local mysd = r(sd)
        local sdstring = round(`mysd',.01) // rounded for display
        qui: outreg2 using "$TABLELOC/`outregfile'.tex", $OUTREG2OPTIONS ctitle("`ctitle',(standardized)") dec(`decimals') addtext("Year-party FEs",$FECHECK,"Party-districting cycle-district FEs",$FECHECK,"Response variable standard deviation",`sdstring') sortvar(`sortvar') stnum(replace coef=coef*`mysd', replace se=se*`mysd',replace ci_low=ci_low*`mysd', replace ci_high=ci_high*`mysd') `repl'
       }
       else {
         qui: outreg2 using "$TABLELOC/`outregfile'.tex", $OUTREG2OPTIONS ctitle("`ctitle'") dec(`decimals') addtext("Year-party FEs",$FECHECK,"Party-districting cycle-district FEs",$FECHECK) sortvar(`sortvar') `repl'
       }
       local repl "append"

      
      ***********************************
      * By competitiveness of district.
      ***********************************
      qui reghdfe `dv' incum_nom_d1 $PRES_SHARE_CONTROL , a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE
      
			* Recode explanatory variable into Gelman 2SD units
			* for easier interpretation.
			standardizeVar2 incum_nom_d1, generate(ev2)
			* New interaction of standardized ev2 with competitive.
			capture drop ev2Xcomp
			qui gen ev2Xcomp = ev2*lag_general_compet
			label var ev2Xcomp "Incumbent centrism*Competitive"          
			* Re-run regression on standardized variable.
			qui reghdfe `dv' ev2 ev2Xcomp $PRES_SHARE_CONTROL pooled_presXcomp, a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE 
       if ($STANDARDIZEDV & inlist("`dv'", "hsscore", "prehsscore" ,"nominee_nominate" ,"recipientcfscore")) {
        * Standardize coefficients.
        qui summ `dv' if e(sample)
        local mysd = r(sd)
        local sdstring = round(`mysd',.01) // rounded for display
        qui: outreg2 using "$TABLELOC/`outregfileByComp'.tex", $OUTREG2OPTIONS ctitle("`ctitle',(standardized)") dec(3) addtext("Party-districting cycle-district FEs",$FECHECK,"Year-party FEs",$FECHECK,"Response variable standard deviation",`sdstring') sortvar(`sortvarcomp') stnum(replace coef=coef*`mysd', replace se=se*`mysd',replace ci_low=ci_low*`mysd', replace ci_high=ci_high*`mysd') `replByComp'
        local replByComp "append"
       }
       else {
        qui:outreg2 using "$TABLELOC/`outregfileByComp'.tex", $OUTREG2OPTIONS ctitle("`ctitle'") dec(`decimals') addtext("Party-districting cycle-district FEs",$FECHECK,"Year-party FEs",$FECHECK) sortvar(`sortvarcomp') `replByComp'
        local replByComp "append"
       }

      ***********************************
      * By nominee party.
      ***********************************
      qui reghdfe `dv' incum_nom_d1 $PRES_SHARE_CONTROL , a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE
      
			* Recode explanatory variable into Gelman 2SD units
			* for easier interpretation.
			standardizeVar2 incum_nom_d1, generate(ev2)
			* New interaction of standardized ev2 with party.
			capture drop ev2Xdem
			qui gen ev2Xdem = ev2*(nominee_party == "D")
			label var ev2Xdem "Incumbent centrism*Democratic primary"          
			* Re-run regression on standardized variable.
			qui reghdfe `dv' ev2 ev2Xdem $PRES_SHARE_CONTROL pooled_presXdem, a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE

			if ($STANDARDIZEDV & inlist("`dv'", "hsscore", "prehsscore" ,"nominee_nominate" ,"recipientcfscore", "mean_party_hsscore")) {
        * Standardize coefficients.
        qui summ `dv' if e(sample)
        local mysd = r(sd)
        local sdstring = round(`mysd',.01) // rounded for display
        qui: outreg2 using "$TABLELOC/`outregfileByParty'.tex", $OUTREG2OPTIONS ctitle("`ctitle',(standardized)") dec(3) addtext("Party-districting cycle-district FEs",$FECHECK,"Year-party FEs",$FECHECK,"Response variable standard deviation",`sdstring') sortvar(`sortvarcomp') stnum(replace coef=coef*`mysd', replace se=se*`mysd',replace ci_low=ci_low*`mysd', replace ci_high=ci_high*`mysd') `replByParty'
        local replByParty "append"
       }
       else {
        qui: outreg2 using "$TABLELOC/`outregfileByParty'.tex", $OUTREG2OPTIONS ctitle("`ctitle'") dec(`decimals') addtext("Party-districting cycle-district FEs",$FECHECK,"Year-party FEs",$FECHECK) sortvar(`sortvarcomp') `replByParty'
        local replByParty "append"
       }

      ***********************************
      * By era.
      ***********************************
      qui reghdfe `dv' incum_nom_d1 $PRES_SHARE_CONTROL , a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE
      
			* Recode explanatory variable into Gelman 2SD units
			* for easier interpretation.
			standardizeVar2 incum_nom_d1, generate(ev2)
			* New interaction of standardized ev2 with party.
			capture drop ev2Xafter94
			qui gen ev2Xafter94 = ev2*(year > 1994)
			label var ev2Xafter94 "Incumbent centrism*After 1994"          
			* Re-run regression on standardized variable.
			qui reghdfe `dv' ev2 ev2Xafter94 $PRES_SHARE_CONTROL pooled_presXafter94, a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE

			if ($STANDARDIZEDV & inlist("`dv'", "hsscore", "prehsscore" ,"nominee_nominate" ,"recipientcfscore", "mean_party_hsscore")) {
        * Standardize coefficients.
        qui summ `dv' if e(sample)
        local mysd = r(sd)
        local sdstring = round(`mysd',.01) // rounded for display
        qui: outreg2 using "$TABLELOC/`outregfileByEra'.tex", $OUTREG2OPTIONS ctitle("`ctitle',(standardized)") dec(3) addtext("Party-districting cycle-district FEs",$FECHECK,"Year-party FEs",$FECHECK,"Response variable standard deviation",`sdstring') sortvar(`sortvarcomp') stnum(replace coef=coef*`mysd', replace se=se*`mysd',replace ci_low=ci_low*`mysd', replace ci_high=ci_high*`mysd') `replByEra'
        local replByEra "append"
       }
       else {
        qui:outreg2 using "$TABLELOC/`outregfileByEra'.tex", $OUTREG2OPTIONS ctitle("`ctitle'") dec(`decimals') addtext("Party-districting cycle-district FEs",$FECHECK,"Year-party FEs",$FECHECK) sortvar(`sortvarcomp') `replByEra'
        local replByEra "append"
       }
    } 
    else {
      di "Skipping dv `dv' because no observations."
    }
} // close dv loop


******************
* Pre-primary ideology models.
******************

* Dependent variable.
local DVS "prehsscore"

* Set up local variables.
local outregfile "`dset'-DiD-prehsscore"
local notes ""
local repl "replace"
di _n "`outregfile'.tex"

* Loop over DVS.
foreach dv of varlist `DVS' {
  
  * Number of decimals.
  if (regexm("`dv'","contribs|support")) {
    local decimals = 1
  }
  else {
    local decimals = 3
  }

  * Column title.
  local ctitle: var label `dv'
	
	********************
	* Models pooled across party.
	********************
	di "Pooled: `dv' incum_nom_d1 `repl'"
	* Don't execute if no data.
	qui summ `dv'
	if (r(N) > 0) {
		
		****************
		* Time and group var FEs.
		****************
		qui reghdfe `dv' incum_nom_d1 $PRES_SHARE_CONTROL, a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE

		* Recode continuous NOMINATE explanatory variable into Gelman 2SD units
		* for easier interpretation.
		standardizeVar2 incum_nom_d1, generate(ev2)
		* Re-run regression on standardized variable.
		qui reghdfe `dv' ev2 $PRES_SHARE_CONTROL, a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE

		 * Outreg.
		 if ($STANDARDIZEDV & inlist("`dv'", "hsscore", "prehsscore" ,"nominee_nominate" ,"recipientcfscore", "mean_party_hsscore")) {
			* Standardize coefficients.
			qui summ `dv' if e(sample)
			local mysd = r(sd)
			local sdstring = round(`mysd',.01) // rounded for display
			qui: outreg2 using "$TABLELOC/`outregfile'.tex", $OUTREG2OPTIONS ctitle("`ctitle',(standardized)") dec(`decimals') addtext("Year-party FEs",$FECHECK,"Party-districting cycle-district FEs",$FECHECK,"Response variable standard deviation",`sdstring') sortvar(`sortvar') stnum(replace coef=coef*`mysd', replace se=se*`mysd',replace ci_low=ci_low*`mysd', replace ci_high=ci_high*`mysd') `repl'
		 }
		 else {
			 qui: outreg2 using "$TABLELOC/`outregfile'.tex", $OUTREG2OPTIONS ctitle("`ctitle'") dec(`decimals') addtext("Year-party FEs",$FECHECK,"Party-districting cycle-district FEs",$FECHECK) sortvar(`sortvar') `repl'
		 }
		 local repl "append"

		
		***********************************
		* By competitiveness of district.
		***********************************
		qui reghdfe `dv' incum_nom_d1 $PRES_SHARE_CONTROL , a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE
		
		* Recode explanatory variable into Gelman 2SD units
		* for easier interpretation.
		standardizeVar2 incum_nom_d1, generate(ev2)
		* New interaction of standardized ev2 with competitive.
		capture drop ev2Xcomp
		qui gen ev2Xcomp = ev2*lag_general_compet
		label var ev2Xcomp "Incumbent centrism*Competitive"          
		* Re-run regression on standardized variable.
		qui reghdfe `dv' ev2 ev2Xcomp $PRES_SHARE_CONTROL pooled_presXcomp, a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE

		if ($STANDARDIZEDV & inlist("`dv'", "hsscore", "prehsscore" ,"nominee_nominate" ,"recipientcfscore", "mean_party_hsscore")) {
			* Standardize coefficients.
			qui summ `dv' if e(sample)
			local mysd = r(sd)
			local sdstring = round(`mysd',.01) // rounded for display
			qui: outreg2 using "$TABLELOC/`outregfile'.tex", $OUTREG2OPTIONS ctitle("`ctitle',(standardized)") dec(3) addtext("Party-districting cycle-district FEs",$FECHECK,"Year-party FEs",$FECHECK,"Response variable standard deviation",`sdstring') sortvar(`sortvarcomp') stnum(replace coef=coef*`mysd', replace se=se*`mysd',replace ci_low=ci_low*`mysd', replace ci_high=ci_high*`mysd') `repl'
			local repl "append"
		 }
		 else {
			qui:outreg2 using "$TABLELOC/`outregfile'.tex", $OUTREG2OPTIONS ctitle("`ctitle'") dec(`decimals') addtext("Party-districting cycle-district FEs",$FECHECK,"Year-party FEs",$FECHECK) sortvar(`sortvarcomp') `repl'
			local repl "append"
		 }

		
		***********************************
		* By era.
		***********************************
		qui reghdfe `dv' incum_nom_d1 $PRES_SHARE_CONTROL , a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE
		
		* Recode explanatory variable into Gelman 2SD units
		* for easier interpretation.
		standardizeVar2 incum_nom_d1, generate(ev2)
		* New interaction of standardized ev2 with party.
		capture drop ev2Xafter94
		qui gen ev2Xafter94 = ev2*(year > 1994)
		label var ev2Xafter94 "Incumbent centrism*After 1994"          
		* Re-run regression on standardized variable.
		qui reghdfe `dv' ev2 ev2Xafter94 $PRES_SHARE_CONTROL pooled_presXafter94, a(time_var grouping_var) keepsingletons noconstant $ROBUST_SE

		if ($STANDARDIZEDV & inlist("`dv'", "hsscore", "prehsscore" ,"nominee_nominate" ,"recipientcfscore", "mean_party_hsscore")) {
			* Standardize coefficients.
			qui summ `dv' if e(sample)
			local mysd = r(sd)
			local sdstring = round(`mysd',.01) // rounded for display
			qui: outreg2 using "$TABLELOC/`outregfile'.tex", $OUTREG2OPTIONS ctitle("`ctitle',(standardized)") dec(3) addtext("Party-districting cycle-district FEs",$FECHECK,"Year-party FEs",$FECHECK,"Response variable standard deviation",`sdstring') sortvar(`sortvarcomp') stnum(replace coef=coef*`mysd', replace se=se*`mysd',replace ci_low=ci_low*`mysd', replace ci_high=ci_high*`mysd') `repl'
			local repl "append"
		 }
		 else {
			qui:outreg2 using "$TABLELOC/`outregfile'.tex", $OUTREG2OPTIONS ctitle("`ctitle'") dec(`decimals') addtext("Party-districting cycle-district FEs",$FECHECK,"Year-party FEs",$FECHECK) sortvar(`sortvarcomp') `repl'
			local repl "append"
		 }
	} 
	else {
		di "Skipping dv `dv' because no observations."
	}
} // close dv loop
